package com.miplus.ccrm.core.service.strategy;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.strategy.CpTraReqVo;
import com.miplus.ccrm.core.controller.vo.strategy.CpTraResVo;
import com.miplus.ccrm.core.pojo.FiCpdoTrace;
import com.miplus.ccrm.core.mapper.FiCpdoTraceMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author pc
* @description 针对表【FI_CPDO_TRACE(任务执行日志)】的数据库操作Service实现
* @createDate 2024-12-21 13:40:37
*/
@Service
public class FiCpdoTraceServiceImpl extends MPJBaseServiceImpl<FiCpdoTraceMapper, FiCpdoTrace>
    implements FiCpdoTraceService{

    @Override
    public IPage<CpTraResVo> traceList(CpTraReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                CpTraResVo.class,
                new MPJLambdaWrapper<FiCpdoTrace>()
                        .selectAs("fun_getnames('CPTASK.DO.TYPE',t.tasktype)",CpTraResVo::getTasktypedsc)
                        .selectAs("fun_getnames('CPTASK.TRACE.STATUS',t.status)",CpTraResVo::getStatusdsc)
                        .selectAs("fun_getnames('PUBLIC.MORAUTO',t.domode)",CpTraResVo::getDomodedsc)
                        .selectAll(FiCpdoTrace.class)
                        .between(JPubUtils.IsNotEmpty(paras.getStartdt()), FiCpdoTrace::getStartdt, JPubUtils.StringToDate(paras.getStartdt()), JPubUtils.StringToDate(paras.getEnddt()))
                        .like(JPubUtils.IsNotEmpty(paras.getName()),FiCpdoTrace::getTaskname,paras.getName())
                        .orderByDesc(FiCpdoTrace::getId)
        );
    }
    @Override
    public Integer writeTrace(String usrid,String taskid,String taskname,String tasktype,String domode,String reqip,String status)
    {
        String myhost= JPubUtils.getLocalIPAddr();
        FiCpdoTrace trace=new FiCpdoTrace();
        trace.setTaskid(taskid);
        trace.setTaskname(taskname);
        trace.setCrusr(usrid);
        trace.setReqhost(reqip);
        trace.setTasktype(tasktype);
        trace.setDomode(domode);
        trace.setDohost(myhost);
        trace.setStatus(status);
        trace.setStartdt(new Date());
        baseMapper.insert(trace);
        return trace.getId();
    }

    @Override
    public Integer writeResult(Integer traceid,String status,String result)
    {
        return baseMapper.update(null, new UpdateWrapper<FiCpdoTrace>()
                .lambda()
                .eq(FiCpdoTrace::getId,traceid)
                .set(FiCpdoTrace::getStatus,status)
                .set(FiCpdoTrace::getDoresult, result)
                .set(FiCpdoTrace::getEnddt, new Date())
        );
    }
}




